package com.chery.media.ui.play.hot

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.chery.media.R
import com.chery.media.contacts.MusicType
import com.chery.media.databinding.FragmentHotBinding
import com.chery.media.library.base.BaseVMFragment
import com.chery.media.library.extend.clickNoRepeat
import com.chery.media.library.extend.loadPicture
import com.chery.media.library.extend.toTime
import com.chery.media.ui.play.playViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class HotFragment : BaseVMFragment<HotViewModel, FragmentHotBinding>() {

    private var isResume = false

    private val hotAdapter by lazy {
        HotAdapter().apply {
            loadMoreModule.isAutoLoadMore = true
            loadMoreModule.isEnableLoadMoreIfNotFullPage = true
            loadMoreModule.setOnLoadMoreListener { viewModel.page++ }
            setOnItemClickListener{adapter, view, position ->
                val item = getItem(position)
                playViewModel.playMusic(position)
            }
        }
    }

    override val layoutId: Int
        get() = R.layout.fragment_hot

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.musicListRecycler.adapter = hotAdapter
    }

    override fun onResume() {
        super.onResume()
        isResume = true
        lifecycleScope.launch(Dispatchers.IO) {
            if (playViewModel.currentType.value == MusicType.HOT_LIST) {
                playViewModel.bluetoothPlayMusic(true)
                playViewModel.servicePlayMusic(true)
                if (viewModel.hotMusicList.value.isNullOrEmpty()) {
                    viewModel.page = 1
                } else {
                    playViewModel.setPlayList(viewModel.hotMusicList.value ?: arrayListOf())
                    playViewModel.setCurrentPlayMusic()
                }
            }
        }
//        if (playViewModel.currentType.value == MusicType.HOT_LIST) {
//            playViewModel.bluetoothPlayMusic(true)
//            playViewModel.servicePlayMusic(true)
//            if (viewModel.hotMusicList.value.isNullOrEmpty()) {
//                viewModel.page = 1
//            } else {
//                playViewModel.setPlayList(viewModel.hotMusicList.value ?: arrayListOf())
//                playViewModel.setCurrentPlayMusic()
//            }
//        }
    }

    override fun initObserve() {
        // 设置热门音乐数据
        viewModel.hotMusicList.observe(this) {
            if (it.size < 100) hotAdapter.loadMoreModule.loadMoreEnd()
            else hotAdapter.loadMoreModule.loadMoreComplete()
            if (viewModel.page == 1) {
                if (it.size > 0) hotAdapter.setList(it)
            } else {
                hotAdapter.addData(it)
            }
            playViewModel.setPlayList(it, viewModel.page != 1)
            playViewModel.setCurrentPlayMusic()
        }
        // 显示当前的音乐信息、更新当前播放的音乐进度
        playViewModel.currentHotPlayMusic.observe(this) {
            if (it.isPlaying == true) {
                mBinding.pause.setImageResource(R.drawable.svg_play_pause)
            } else {
                mBinding.pause.setImageResource(R.drawable.svg_play)
            }
            mBinding.musicName.text = it.musicName
            mBinding.musicAuthor.text = it.musicAuthor
            val totalTime = (it.totalTime ?: "0").toFloat()
            mBinding.totalTime.text = (it.totalTime ?: "0").toTime()
            mBinding.musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
            if (playViewModel.currentType.value == MusicType.BLUETOOTH) {
                mBinding.musicBluetoothLyrics.text = ""
            } else {
                mBinding.musicBluetoothLyrics.text = ""
            }
            mBinding.playPosition.text = (it.playPosition ?: "0").toTime()
            val seekBarProgress = ((it.playPosition ?: "0").toFloat() / totalTime) * 100
            mBinding.musicSeeKBar.progress = seekBarProgress.toInt()
        }
    }

    override fun initListener() {
        // 上一首
        mBinding.previousSong.clickNoRepeat(interval = 0) {
            playViewModel.playPrevious()
        }
        // 播放暂停
        mBinding.pause.clickNoRepeat(interval = 0) {
            playViewModel.continuePlayOrPause()
        }
        // 下一首
        mBinding.nextSong.clickNoRepeat(interval = 0) {
            playViewModel.playNext()
        }
        // 播放列表
        mBinding.playList.clickNoRepeat(interval = 0) {
            if (mBinding.musicListRecycler.isVisible) {
                mBinding.infoLayout.isVisible = true
                mBinding.musicListRecycler.isVisible = false
            } else {
                mBinding.infoLayout.isVisible = false
                mBinding.musicListRecycler.isVisible = true
            }
        }
    }

    companion object {
        fun newInstance() = HotFragment()
    }

}